﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportService2Ref;
    using WMS6.Web.UserControls;

    public class OperatorWorkRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15732;
        protected GridView gvOperatorWork;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnPara;
        protected HtmlInputButton htmlbtnReport;
        protected Label lblMsg;
        protected Label lblOperatorWorkRptList;
        protected LinkButton lnkbtnPrintOperatorWork;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected HtmlTableCell tdlnkbtnPrintOperatorWork;
        protected UpdatePanel up1;

        private void BindData2OperatorWorkList(OperatorWorkRptQueryEntity operatorWorkRptQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(operatorWorkRptQueryEntity.CurrentPage);
            DataSet ds = AnalyticsReportService2ClientProxy.GetInstance().GetOperatorWorkRptDataSet(operatorWorkRptQueryEntity);
            this.gvOperatorWork.Visible = true;
            base.BindData2GridView(this.gvOperatorWork, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            OperatorWorkRptQueryEntity operatorWorkRptQueryEntity = this.GetOperatorWorkQueryEntity();
            operatorWorkRptQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = operatorWorkRptQueryEntity.CurrentPage;
            operatorWorkRptQueryEntity.CurrentPage = "0";
            try
            {
                using (DataSet ds = AnalyticsReportService2ClientProxy.GetInstance().GetOperatorWorkRptDataSet(operatorWorkRptQueryEntity))
                {
                    base.Export2Excel("OperatorWorkRpt.xls", "15732", this.gvOperatorWork, ds);
                }
            }
            finally
            {
                operatorWorkRptQueryEntity.IsGetAll = false;
                operatorWorkRptQueryEntity.CurrentPage = currentPageIndex;
                this.SetEmployeeWorkQueryEntity(operatorWorkRptQueryEntity);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            OperatorWorkRptQueryEntity operatorWorkRptQueryEntity = this.GetOperatorWorkQueryEntity();
            operatorWorkRptQueryEntity.CurrentPage = "0";
            this.BindData2OperatorWorkList(operatorWorkRptQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            OperatorWorkRptQueryEntity operatorWorkRptQueryEntity = this.GetOperatorWorkQueryEntity();
            operatorWorkRptQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2OperatorWorkList(operatorWorkRptQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            OperatorWorkRptQueryEntity operatorWorkRptQueryEntity = this.GetOperatorWorkQueryEntity();
            operatorWorkRptQueryEntity.Operator_ID = "";
            operatorWorkRptQueryEntity.StartDateTime = "";
            operatorWorkRptQueryEntity.EndDateTime = "";
            this.BindData2OperatorWorkList(operatorWorkRptQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            OperatorWorkRptQueryEntity operatorWorkRptQueryEntity = this.GetOperatorWorkQueryEntity();
            if (int.Parse(operatorWorkRptQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                operatorWorkRptQueryEntity.CurrentPage = (int.Parse(operatorWorkRptQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2OperatorWorkList(operatorWorkRptQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            OperatorWorkRptQueryEntity operatorWorkRptQueryEntity = this.GetOperatorWorkQueryEntity();
            if (int.Parse(operatorWorkRptQueryEntity.CurrentPage) > 0)
            {
                operatorWorkRptQueryEntity.CurrentPage = (int.Parse(operatorWorkRptQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2OperatorWorkList(operatorWorkRptQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            OperatorWorkRptQueryEntity operatorWorkRptQueryEntity = this.GetOperatorWorkQueryEntity();
            this.BindData2OperatorWorkList(operatorWorkRptQueryEntity);
        }

        private OperatorWorkRptQueryEntity GetOperatorWorkQueryEntity()
        {
            return (base.PageQueryEntitySession as OperatorWorkRptQueryEntity);
        }

        private DataSet GetPrintDataset()
        {
            DataSet ds = null;
            OperatorWorkRptQueryEntity operatorWorkRptQueryEntity = this.GetOperatorWorkQueryEntity();
            operatorWorkRptQueryEntity.IsGetAll = true;
            string currentPageIndex = operatorWorkRptQueryEntity.CurrentPage;
            operatorWorkRptQueryEntity.CurrentPage = "0";
            try
            {
                ds = AnalyticsReportService2ClientProxy.GetInstance().GetOperatorWorkRptDataSet(operatorWorkRptQueryEntity);
            }
            finally
            {
                operatorWorkRptQueryEntity.IsGetAll = false;
                operatorWorkRptQueryEntity.CurrentPage = currentPageIndex;
                this.SetEmployeeWorkQueryEntity(operatorWorkRptQueryEntity);
            }
            return ds;
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                OperatorWorkRptQueryEntity operatorWorkRptQueryEntity = this.GetOperatorWorkQueryEntity();
                operatorWorkRptQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2OperatorWorkList(operatorWorkRptQueryEntity);
            }
        }

        protected void gvOperatorWork_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15732", this.gvOperatorWork);
        }

        protected void gvOperatorWork_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
            }
        }

        protected void gvOperatorWork_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                OperatorWorkRptQueryEntity operatorWorkRptQueryEntity = this.GetOperatorWorkQueryEntity();
                operatorWorkRptQueryEntity.SortField = e.SortExpression;
                if (operatorWorkRptQueryEntity.SortDirection == "ASC")
                {
                    operatorWorkRptQueryEntity.SortDirection = "DESC";
                }
                else if (operatorWorkRptQueryEntity.SortDirection == "DESC")
                {
                    operatorWorkRptQueryEntity.SortDirection = "ASC";
                }
                this.BindData2OperatorWorkList(operatorWorkRptQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void lnkbtnPrintOperatorWork_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = this.GetPrintDataset();
                if ((ds != null) && (ds.Tables[0].Rows.Count > 0))
                {
                    this.Session["PrintOperatorWork"] = ds;
                    OperatorWorkRptQueryEntity operatorWorkRptQueryEntity = this.GetOperatorWorkQueryEntity();
                    string date = string.IsNullOrEmpty(operatorWorkRptQueryEntity.StartDateTime) ? "" : Convert.ToDateTime(operatorWorkRptQueryEntity.StartDateTime).ToString("yyyy-MM-dd");
                    string para = date + " 至 " + (string.IsNullOrEmpty(operatorWorkRptQueryEntity.EndDateTime) ? "" : Convert.ToDateTime(operatorWorkRptQueryEntity.EndDateTime).ToString("yyyy-MM-dd"));
                    this.hdnPara.Value = para;
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>popPrintEmployeeWork();</script>", false);
                }
            }
            catch
            {
            }
        }

        private void LoadData()
        {
            OperatorWorkRptQueryEntity operatorWorkRptQueryEntity = new OperatorWorkRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    operatorWorkRptQueryEntity.Operator_ID = this.PreviousPage.OperatorID;
                    operatorWorkRptQueryEntity.StartDateTime = this.PreviousPage.StartDate;
                    operatorWorkRptQueryEntity.EndDateTime = this.PreviousPage.EndDate;
                }
                else
                {
                    operatorWorkRptQueryEntity.Operator_ID = "";
                    operatorWorkRptQueryEntity.StartDateTime = "";
                    operatorWorkRptQueryEntity.EndDateTime = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            operatorWorkRptQueryEntity.IsGetAll = false;
            operatorWorkRptQueryEntity.CurrentPage = "0";
            operatorWorkRptQueryEntity.PageSize = this.gvOperatorWork.PageSize.ToString();
            operatorWorkRptQueryEntity.SortField = " USER_NAME";
            operatorWorkRptQueryEntity.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvOperatorWork.PageSize;
            this.SetEmployeeWorkQueryEntity(operatorWorkRptQueryEntity);
            this.BindData2OperatorWorkList(operatorWorkRptQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetEmployeeWorkQueryEntity(OperatorWorkRptQueryEntity operatorWorkRptQueryEntity)
        {
            base.PageQueryEntitySession = operatorWorkRptQueryEntity;
        }

        public OperatorWorkRptQuery PreviousPage
        {
            get
            {
                return (OperatorWorkRptQuery) base.PreviousPage;
            }
        }
    }
}

